package fr.fermeManager.application.views.model;
 
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import fr.fermeManager.library.model.beans.TypeTransaction;
 
/**
 *
 */
public class ModelTree implements TreeModel {
	
    private TypeTransaction racine;
    
    private List<TypeTransaction> listeTypeTransaction = new ArrayList<TypeTransaction>();
    
    public ModelTree(){
    	racine = new TypeTransaction(true);
    	racine.setLibelle("racine");
    	racine.setId(0);
    	racine.setIdParent(null);
    }
    
    public ModelTree(List<TypeTransaction> listeTypeTransaction) {
		this.listeTypeTransaction = listeTypeTransaction;
		racine = rechercherRacine();
	}

	@Override
	public Object getRoot() {
		return racine;
	}

	@Override
	public Object getChild(Object parent, int index) {
		List<TypeTransaction> listeTmp = new ArrayList<TypeTransaction>();
		TypeTransaction parentTmp = ((TypeTransaction)parent);
		
		for (TypeTransaction typeTransactionTmp : listeTypeTransaction) {
			if(typeTransactionTmp.getIdParent() != null){
				if(parentTmp.getId() == typeTransactionTmp.getIdParent().intValue()){
					listeTmp.add(typeTransactionTmp);
				}
			}
		}
		return listeTmp.get(index);
	}

	@Override
	public int getChildCount(Object parent) {
		int countTmp = 0;
		for (TypeTransaction typeTransactionTmp : listeTypeTransaction) {
			if(typeTransactionTmp.getIdParent()!=null){
				if(typeTransactionTmp.getIdParent().intValue() == ((TypeTransaction)parent).getId()){
					countTmp++;
				}
			}
		}
		return countTmp;
	}

	@Override
	public boolean isLeaf(Object node) {
		int countTmp = 0;
		TypeTransaction parentTmp = ((TypeTransaction)node);
		
		for (TypeTransaction typeTransactionTmp : listeTypeTransaction) {
			if(typeTransactionTmp.getIdParent() != null){
				if(parentTmp.getId() == typeTransactionTmp.getIdParent()){
					countTmp++;
				}
			}
		}
		return (countTmp==0);
	}

	@Override
	public void valueForPathChanged(TreePath path, Object newValue) {
		System.out.println("ssssssssssss");
		
	}

	@Override
	public int getIndexOfChild(Object parent, Object child) {
		
		TypeTransaction parentTmp = ((TypeTransaction)parent);
		List<TypeTransaction> listeChildren = new ArrayList<TypeTransaction>();
		
		for (TypeTransaction typeTransacTmp : listeTypeTransaction) {
			if(typeTransacTmp.getIdParent() !=null) {
				if(typeTransacTmp.getIdParent() == parentTmp.getId()){
					listeChildren.add(typeTransacTmp);
				}
			}
		}
		
		return listeChildren.indexOf(child);
	}
	
	private List<TypeTransaction> getChildren(TypeTransaction typeTransacTmp){
		List<TypeTransaction> listeEnfants = new ArrayList<TypeTransaction>();
		for (TypeTransaction typeTransaction : listeTypeTransaction) {
			if(typeTransaction.getIdParent()!=null){
				if(typeTransaction.getIdParent() == typeTransacTmp.getId()){
					listeEnfants.add(typeTransaction);
				}
			}
		}
		return listeEnfants;
	}

	@Override
	public void addTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return the racine
	 */
	public TypeTransaction getRacine() {
		return racine;
	}

	/**
	 * @param racine the racine to set
	 */
	public void setRacine(TypeTransaction racine) {
		this.racine = racine;
	}

	/**
	 * @return the listeTypeTransaction
	 */
	public List<TypeTransaction> getListeTypeTransaction() {
		return listeTypeTransaction;
	}

	/**
	 * @param listeTypeTransaction the listeTypeTransaction to set
	 */
	public void setListeTypeTransaction(List<TypeTransaction> listeTypeTransaction) {
		this.listeTypeTransaction = listeTypeTransaction;
		racine = rechercherRacine();
	}
	
	private TypeTransaction rechercherRacine(){
		for (TypeTransaction typeTransacTmp : this.listeTypeTransaction) {
			if(typeTransacTmp.getIdParent()==null){
				return typeTransacTmp;
			}
		}
		return null;
	}
	
	public void expandAll(JTree tree, boolean expand) {
		TypeTransaction root = (TypeTransaction)tree.getModel().getRoot();
		expandAll(tree, new TreePath(root), expand,0);
	}
	private void expandAll(JTree tree, TreePath parent, boolean expand,int level) {
	    // Traverse children
		TypeTransaction node = (TypeTransaction)parent.getLastPathComponent();
	    if (getChildCount(node) >= 0) {
	    	
	    	for (TypeTransaction typeTransacTmp : getChildren(node)) {
	    		TreePath path = parent.pathByAddingChild(typeTransacTmp);
	    		expandAll(tree, path, expand,level+1);
			}
	    }
	    // Expansion or collapse must be done bottom-up
	    if(level == 0){
	    	tree.expandPath(parent);
	    }else{
		    if (expand) {
		        tree.expandPath(parent);
		    } else {
		        tree.collapsePath(parent);
		    }
	    }
	}

}